﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace MainWebService
{
    /// <summary>
    /// Summary description for Services
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Services : System.Web.Services.WebService
    {
        /*
        [WebMethod]
        public string SyncDataToDevice1(int serviceId, int userId, int clientId, int hashCode)
        {
            string fileData = "";
            ServicesForData ss = new ServicesForData();
            fileData = ss.GetScanDataCSV(serviceId, userId, clientId, hashCode);
            return fileData;
        }

        [WebMethod]
        public void SyncDataToDevice2(int serviceId, int userId, int clientId, int hashCode)
        {
            string fileData = "";
            ServicesForData ss = new ServicesForData();
            fileData = ss.GetScanDataCSV(serviceId,userId,clientId,hashCode);
            FileObject _obj = new FileObject();
            _obj.fileBytes = System.Text.Encoding.ASCII.GetBytes(fileData);
            _obj.fileExtension = ".csv";
            _obj.fileType = "text/csv";
            ss.SendFile(_obj);
        }

        [WebMethod]
        public string SyncDataFromDevice1(int serviceId, int userId, int clientId, int hashCode, string csvData)
        {
            ServicesForData _srv = new ServicesForData();
           // return _srv.SaveFromDevice(serviceId, userId, clientId, hashCode);
            FileObject _obj = new FileObject();
            _obj.fileBytes = System.Text.Encoding.ASCII.GetBytes(csvData);
            _obj.fileExtension = ".csv";
            _obj.fileType = "text/csv";
            return SaveFileData(serviceId, userId, clientId, hashCode, _obj);
        }
              

        [WebMethod]
        public void SyncDataFromDevice2(int serviceId, int userId, int clientId, int hashCode, FileObject fileObj)
        {
            SaveFileData(serviceId, userId, clientId, hashCode, fileObj);
        }

        private static string SaveFileData(int serviceId, int userId, int clientId, int hashCode, FileObject fileObj)
        {
            string msg = "";
            try
            {
                ServicesForData _sData = new ServicesForData();
                ScanDataFile _data = null;
                StreamReader reader = new StreamReader(new MemoryStream(fileObj.fileBytes));
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    if (!String.IsNullOrWhiteSpace(line))
                    {
                        string[] values = line.Split(',');
                        if (values.Length == 3)
                        {
                            _data = new ScanDataFile();
                            _data.Value = values[0];
                            _data.Response = values[1];
                            _data.validity = values[2];
                            _sData.AddData(serviceId, userId, clientId, hashCode, _data);
                        }
                    }
                }
                msg = "0";
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return msg;
        }
        
        [WebMethod]
        public string AddScanDataLive(int serviceId, int userId, int clientId, int hashCode, ScanDataFile scanData)
        {
            ServicesForData _sData = new ServicesForData();
            return _sData.AddData(serviceId, userId, clientId, hashCode, scanData);
        }
         * */

        #region RecordScans
       
        
        public List<Scans> SyncDataToDevice(int clientId, int userId, string hashCode, int serviceId)
        {
            return new ScanDetails(clientId, userId, hashCode, serviceId).GetScans();
        }

        [WebMethod]
        public bool SyncDataFromDevice(int clientId, int userId, string hashCode, List<Scans> lstScans)
        {
            int scanId = 0;
            if (lstScans != null && lstScans.Count > 0)
            {
                for (int i = 0; i < lstScans.Count; i++)
                {
                    ScanDetails.AddScanData(clientId, userId, hashCode, lstScans[i], ref scanId);
                }
                return true;
            }
            else
                return false;
        }

        [WebMethod]
        public bool ScanDataOnline(int clientId, int userId, string hashCode, Scans _scanData)
        {
            int scanId = 0;
            ScanDetails.AddScanData(clientId, userId, hashCode, _scanData,ref scanId);
            return scanId > 0;
        } 
        #endregion

        #region ValidateScans
        [WebMethod]
        public Scans ValidateOnlineScans(int userId,int clientId,string hashCode,int ServiceId,string StudentIdNumber,string DeviceId, string DeviceName)
        {
            return ScanDetails.ValidateScan(userId, clientId, hashCode, ServiceId, StudentIdNumber,DeviceId,DeviceName);
        }
        [WebMethod]
        public List<Scans> ValidateOfflineScans(int userId, int clientId, string hashCode, int ServiceId,string DeviceId, string DeviceName)
        {
            return ScanDetails.GetDataforOfflineValidation(userId, clientId, hashCode, ServiceId, DeviceId, DeviceName);
         
        }
        #endregion

        #region Database Builder
        public Scans DatabaseBuilderGetScans(int clientId,int userId,int serviceId,string hashCode,int StudentIdNumber,DeviceInfo device)
        {
            return ScanDetails.DatabaseBuilder(clientId, userId, serviceId,StudentIdNumber, hashCode,device);
        }
        public bool DatabaseBuilderSaveScans(int clientId, int userId,string hashCode, Scans scan)
        {
            return ScanDetails.DatabaseBuilderSaveScans(clientId, userId, hashCode, scan);
        } 
        #endregion

        [WebMethod]
        public List<Scans> SearchScanByName(int clientId,int serviceId,string hashCode,string searchString)
        {
            return ScanDetails.SearchScan(clientId,serviceId, hashCode, searchString);
        }
        [WebMethod]
        public string DeleteService(int serviceId,string hashCode)
        {
            return new ServiceOperations().DeleteService(serviceId,hashCode);
        }


        [WebMethod]
        public string AssociateQuestions2Service(Questions question,int serviceId)
        {
            return new QuestionOperations().AssociateQuestion(question,serviceId);
        }
    }
}
